#[[
   FindMyLib.cmake
   查找 MyLib 库

   该模块设置以下变量：
      MyLib_FOUND - 如果找到 MyLib 库，则为 TRUE
      MyLib_INCLUDE_DIRS - MyLib 库的头文件目录
      MyLib_LIBRARIES - MyLib 库的库文件路径
      MyLib_VERSION - MyLib 库的版本号
]]#

# 查找头文件
find_path(
    MyLib_INCLUDE_DIR
    NAMES
        mylib/mylib.h
    PATHS
        /usr/local/include
        /usr/include
        /home/fly/workspace/mylib/include
    DOC "MyLib 头文件所在的目录"
)

# 查找库文件
find_library(
    MyLib_LIBRARY
    NAMES
        MyLib
    PATHS
        /usr/local/lib
        /usr/lib
        /home/fly/workspace/mylib/lib
    DOC "MyLib 库文件路径"
)

# 查找版本文件
find_file(
    MyLib_VERSION_FILE
    NAMES
        MyLibConfigVersion.cmake
    PATHS
        /usr/local/lib/cmake/MyLib
        /home/fly/workspace/mylib/lib/cmake/MyLib
    DOC "MyLib 版本文件路径"
)

# 处理查找结果
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
    MyLib
    REQUIRED_VARS
        MyLib_INCLUDE_DIR
        MyLib_LIBRARY
    VERSION_VAR MyLib_VERSION  # 指定版本变量 (如果版本信息在 MyLibConfigVersion.cmake 中设置了 MyLib_VERSION)
)

# 设置输出变量
if(MyLib_FOUND)
    set(MyLib_INCLUDE_DIRS ${MyLib_INCLUDE_DIR})
    set(MyLib_LIBRARIES ${MyLib_LIBRARY})

    # 读取版本信息
    if(MyLib_VERSION_FILE)
        if(EXISTS "${MyLib_VERSION_FILE}") # 确保文件存在
            include("${MyLib_VERSION_FILE}") # 将 MyLib_VERSION 设置为版本文件中的值
            message(STATUS "MyLib 版本文件: ${MyLib_VERSION_FILE}")

            # 检查 MyLib_VERSION 是否被设置
            if(DEFINED MyLib_VERSION)
                message(STATUS "MyLib 版本: ${MyLib_VERSION}")
            else()
                message(STATUS "MyLib 版本: 未在 MyLibConfigVersion.cmake 中定义")
            endif()
        else()
            message(WARNING "MyLib version file not found: ${MyLib_VERSION_FILE}")
        endif()
    endif()

    if(NOT MyLib_FIND_QUIETLY)
        message(STATUS "找到 MyLib 库: ${MyLib_LIBRARIES}")
    endif()
else()
    if(MyLib_FIND_REQUIRED)
        message(FATAL_ERROR "找不到 MyLib 库")
    endif()
endif()

mark_as_advanced(MyLib_INCLUDE_DIR MyLib_LIBRARY MyLib_VERSION_FILE)